#include "StdAfx.h"
#include "ModulatorThread.h"
#include "../Vocoder/Vocoder.h"

ModulatorThread::ModulatorThread(int windowLength)
{
	this->windowLength = windowLength;
	curSample.resize(windowLength);
	isRunning = false;
}

ModulatorThread::~ModulatorThread(void)
{
}

void ModulatorThread::Start()
{
	sf::Thread::Launch();
}

void ModulatorThread::Run()
{
	isRunning = true;
	while(isRunning)
	{
		PushAvailableSamples();
		Sleep(10);
	}
}

void ModulatorThread::PushAvailableSamples()
{
	while(buffer.size()>windowLength)
	{
		queueMutex.Lock();
		for(int i=0; i<windowLength; i++)
		{
			short cur = buffer.front();
			buffer.pop();
			curSample[i] = cur;
		}
		queueMutex.Unlock();
		vocoder->Vocode(&curSample[0], windowLength);
	}
}

void ModulatorThread::Stop()
{
	isRunning = false;
}

void ModulatorThread::NewSample(short* sample, int length)
{
	queueMutex.Lock();
	for(int i=0; i<length; i++)
	{
		buffer.push(sample[i]);
	}
	queueMutex.Unlock();
}



